﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chilis.CRM.TransferObjects;
using Chilis.CRM.DataAccess;

namespace Chilis.CRM.Services
{
    public class ClientesService
    {

        
        public String ListarClientesRFM()
        {
            IList<Cliente> listaClientes = DCliente.Instancia.ListarClientesRFM();
            String json = "{ \"chart\": { \"caption\" : \"Analisis RFM\" , " +
                          "\"xAxisName\" : \"Variables RFM\", \"yAxisName\" : \"Valores\", \"numberPrefix\" : \"\" },\"data\" : [ ";
            String data ="";
            Decimal cont = 0;
            var sumaRecency = 0;
            var sumaFrecuency = 0;
            var sumaMonetary = 0;
                    
            foreach (Cliente c in listaClientes){
                sumaFrecuency += c.Frecuency;
                sumaRecency += c.Recency;
                sumaMonetary = Convert.ToInt32(sumaMonetary) + Convert.ToInt32(c.Monetary);
            }

            data = "{ \"label\" : \"Frecuency\", \"value\" : \"" + (sumaFrecuency/listaClientes.Count) + "\" },  " +
                       "{ \"label\" : \"Recency\", \"value\" : \"" + (sumaRecency / listaClientes.Count) + "\" }, " +
                       "{ \"label\" : \"Monetary\", \"value\" : \"" + (sumaMonetary / listaClientes.Count) + "\" }]}"; 

            //foreach(Cliente c in listaClientes){
            //    cont++;               
            //    data = "{ \"label\" : \"Frecuency\", \"value\" : \"" + c.Frecuency + "\" },  " +
            //           "{ \"label\" : \"Recency\", \"value\" : \"" + c.Recency + "\" }, " +
            //           "{ \"label\" : \"Monetary\", \"value\" : \"" + c.Monetary + "\" }"; 
            //    if(cont<listaClientes.Count)
            //          data+=",";
            //    json+=data;                
            //}
            //return json+"]}";
            return json+data;
        }
        
    }
}
